package smartcrud.spring.controllers.cms;

import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import smartcrud.common.orm.Page;
import smartcrud.common.orm.PropertyFilter;
import smartcrud.common.orm.jdbc.JdbcUtils;
import smartcrud.spring.controllers.cms.editor.AttachmentEditor;
import smartcrud.spring.models.cms.Attachment;
import smartcrud.spring.models.cms.CategoryService;
import smartcrud.spring.models.cms.Post;
import smartcrud.spring.models.cms.PostService;
/**
 * Rails 1.2中对REST的实现默认把controller中的方法限制在7个：<br>
 * index、show、new、edit、create、 update和destroy.<br>
 * 而根据SpringSide4,使用Restful风格的Urls:<br>
 * 
 * <pre>
 * List page     : GET  /post/ 
 * Show page     : GET  /post/{id}
 * Create page   : GET  /post/create
 * Create action : POST /post/create
 * Update page   : GET  /post/update/{id}
 * Update action : POST /post/update
 * Delete action : GET  /post/delete
 * </pre>
 * 
 * @author cyper.yin
 * 
 */
@Controller
@RequestMapping(value = "/post")
public class PostController {
	private static final String SUCCESS = "redirect:/post/";
	
	@Autowired
	private PostService postService;
	
	@Autowired
	private CategoryService categoryService;

	@RequestMapping(value = "")
	public String index(
			@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
			Model model, 
			HttpServletRequest request) {
		
		List<PropertyFilter> filters = JdbcUtils.buildPropertyFilters(request);
		
		Page<Post> page = new Page<Post>(10);
		page.setPageIndex(pageIndex);
		page = postService.findByPage(page,filters);
		
		model.addAttribute("page", page);
		
		return "post/post_index";
	}

	@RequestMapping("/{id}")
	public String show(Model model, @PathVariable("id") Long id) {
		Post post = postService.findPostById(id);
		model.addAttribute(post);

		return "post/post_show";
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(ModelMap model) {
        model.addAttribute(categoryService.findAllCategorys());
        model.addAttribute("tempPostId",UUID.randomUUID().toString());
		
		Post post = new Post();
		post.setHit(0);
		post.setDing(0);
		post.setCai(0);
		post.getAttachments().add(0,Attachment.nullAttachment());
		
		model.addAttribute(post);
		return "post/post_new";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(
			HttpSession session,
			@ModelAttribute("post") Post post,
			RedirectAttributes redirectAttributes) {
		
		post.setUserId((Long) session.getAttribute("userid"));
		postService.createPost(post);
		
		redirectAttributes.addFlashAttribute("message", "create success!");

		return SUCCESS;
	}
	
    @InitBinder
    protected void initBinder(WebDataBinder binder) throws ServletException {
        binder.registerCustomEditor(Attachment.class, new AttachmentEditor());
    }
	
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(Model model, @PathVariable("id") Long id) {
		 model.addAttribute(categoryService.findAllCategorys());
		 
		Post post = postService.findPostById(id);
		post.getAttachments().add(0,Attachment.nullAttachment());
		model.addAttribute(post);
		return "post/post_edit";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@ModelAttribute("post") Post post,
			RedirectAttributes redirectAttributes) {
		postService.updatePost(post);
		redirectAttributes.addFlashAttribute("message", "update success!");
		return SUCCESS;
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id,
			RedirectAttributes redirectAttributes) {
		postService.deletePost(id);
		redirectAttributes.addFlashAttribute("message", "delete success!");
		return SUCCESS;
	}
}
